package com.android.yummycookies.loader;

import android.app.Activity;
import android.os.Handler;
import android.view.View;
import android.widget.FrameLayout;

import com.android.yummycookies.service.storer.imagesStorer.binder.BitmapsStoreBinder;

public abstract class AbstractLoader {

	private final Activity activity;
	private final BitmapsStoreBinder bitmapsStore;
	private final View loadingScreen;

	private final Handler observer;

	private final FrameLayout rootLayout;

	public AbstractLoader(Activity activity, BitmapsStoreBinder bitmapsStore,
			View loadingScreen) {
		this.activity = activity;
		this.bitmapsStore = bitmapsStore;
		this.loadingScreen = loadingScreen;

		observer = createObserver();

		rootLayout = (FrameLayout) activity.findViewById(android.R.id.content);

		startLoading();
	}
	
	public Activity getActivity() {
		return activity;
	}
	
	public BitmapsStoreBinder getBitmapsStore() {
		return bitmapsStore;
	}
	
	

	public abstract Handler createObserver();

	public void startLoading() {
		rootLayout.addView(loadingScreen);
		bitmapsStore.getObserversPool().addObserver(observer);
		load();
	}

	public void finishLoading() {
		bitmapsStore.getObserversPool().removeObserver(observer);
		rootLayout.removeViewAt(rootLayout.getChildCount() - 1);
	}

	public abstract void load();

}
